import React, { Component } from "react";
import "./index.css";
export default class Item extends Component {
  state = { mouse: false };
  handleMouse = (falg) => {
    return () => {
      this.setState({ mouse: falg });
    };
  };
  handleCheck = (id) => {
    return (event) => {
      console.log(id, event.target.checked);
      this.props.updateTodo(id, event.target.checked);
    };
  };
  heandleClear = (id) => {
    console.log("删除 id = ", id);
    if (window.confirm("确定删除吗？")) {
      this.props.deleteTodo(id);
    }
  };
  render() {
    const { item } = this.props;
    return (
      <div>
        <li
          style={{ backgroundColor: this.state.mouse ? "#ddd" : "white" }}
          onMouseEnter={this.handleMouse(true)}
          onMouseLeave={this.handleMouse(false)}
        >
          <label>
            <input
              type="checkbox"
              checked={item.done}
              onChange={this.handleCheck(item.id)}
            />
            <span>{item.name}</span>
          </label>
          <button
            onClick={() => {
              this.heandleClear(item.id);
            }}
            className="btn btn-danger"
            style={{ display: this.state.mouse ? "" : "none" }}
          >
            删除
          </button>
        </li>
      </div>
    );
  }
}
